#include "adc.h"

uint16_t Volt_Buffer[ADC_SAMPLE_SIZE];
uint16_t Vout_Buffer[ADC_SAMPLE_SIZE];
uint16_t V12V_Buffer[ADC_SAMPLE_SIZE];
uint16_t Vref_Buffer[ADC_SAMPLE_SIZE];
uint16_t NTC_Buffer[ADC_SAMPLE_SIZE];
uint16_t MCUTs_Buffer[ADC_SAMPLE_SIZE];

uint8_t overcurr_state;
void ADC_init(void)
{
    //	unsigned int i;
    ADC_WatchdogTypeDef ADC_WdtStructure = {0};
    // 打开GPIO时钟
    __SYSCTRL_GPIOA_CLK_ENABLE();
    // 打开ADC时钟
    __SYSCTRL_ADC_CLK_ENABLE();

    // set PA01 as AIN1 INPUT
    PA01_ANALOG_ENABLE();
    PA02_ANALOG_ENABLE();
    PA03_ANALOG_ENABLE();
    PA11_ANALOG_ENABLE();

    REGBITS_SET(CW_ADC->CR, ADC_CR_BGREN_Msk); // 使能1.2V BGR

    // ADC模拟看门狗通道初始化
    ADC_WdtStructure.ADC_InitStruct.ADC_ClkDiv = ADC_Clk_Div8;//48/8=6M
    ADC_WdtStructure.ADC_InitStruct.ADC_ConvertMode = ADC_ConvertMode_Continuous;
    ADC_WdtStructure.ADC_InitStruct.ADC_SQREns = ADC_SqrEns0to5;                  // 单通道
    ADC_WdtStructure.ADC_InitStruct.ADC_IN0.ADC_InputChannel = ADC_InputCH1;      // 电流偏执电压
    ADC_WdtStructure.ADC_InitStruct.ADC_IN0.ADC_SampTime = ADC_SampTime102Clk;    // 15+54=69 6MHZ/69=86.9K = 11.5us
    ADC_WdtStructure.ADC_InitStruct.ADC_IN1.ADC_InputChannel = ADC_InputCH2;      // 电流
    ADC_WdtStructure.ADC_InitStruct.ADC_IN1.ADC_SampTime = ADC_SampTime54Clk;
    ADC_WdtStructure.ADC_InitStruct.ADC_IN2.ADC_InputChannel = ADC_InputCH3;     // 电压
    ADC_WdtStructure.ADC_InitStruct.ADC_IN2.ADC_SampTime = ADC_SampTime54Clk;
    ADC_WdtStructure.ADC_InitStruct.ADC_IN3.ADC_InputChannel = ADC_InputVref1P2; // 1.2V，采样时间不小于40US 15+390= 405 6M / 405 = 14.8k=67.5us
    ADC_WdtStructure.ADC_InitStruct.ADC_IN3.ADC_SampTime = ADC_SampTime390Clk;
    ADC_WdtStructure.ADC_InitStruct.ADC_IN4.ADC_InputChannel = ADC_InputCH13; // NTC温度
    ADC_WdtStructure.ADC_InitStruct.ADC_IN4.ADC_SampTime = ADC_SampTime54Clk;
    ADC_WdtStructure.ADC_InitStruct.ADC_IN5.ADC_InputChannel = ADC_InputTs;    // 芯片内置温度
    ADC_WdtStructure.ADC_InitStruct.ADC_IN5.ADC_SampTime = ADC_SampTime390Clk; // 15+390= 405 6M / 405 = 14.8k=67.5us
    ADC_WdtStructure.ADC_WatchdogCHx = ADC_WATCHDOG_IN2;    // 模拟看门狗监控的是输入通道
    ADC_WdtStructure.ADC_WatchdogOverHighIrq = ENABLE;
    ADC_WdtStructure.ADC_WatchdogUnderLowIrq = ENABLE;
    ADC_WdtStructure.ADC_WatchdogVth = 2300;
    ADC_WdtStructure.ADC_WatchdogVtl = 1700;

    ADC_WatchdogInit(&ADC_WdtStructure);
    REGBITS_SET(CW_ADC->CR, ADC_CR_BGREN_Msk); // 使能1.2V BGR

    ADC_SetTs(ADC_TsEnable);

    ADC_ITConfig(ADC_IT_AWDH | ADC_IT_AWDL, DISABLE);
    ADC_ClearITPendingAll(); // 清除ADC所有中断状态
    NVIC_EnableIRQ(ADC_IRQn);
    ADC_Enable();                     // ADC使能
    ADC_SoftwareStartConvCmd(ENABLE); // ADC转换软件启动命令
}

void ADC_Result_Scan(void)
{
    static uint8_t cnt;

    ADC_GetSqr0Result(&Vref_Buffer[cnt]);
    ADC_GetSqr1Result(&Vout_Buffer[cnt]);
    ADC_GetSqr2Result(&Volt_Buffer[cnt]);
    ADC_GetSqr3Result(&V12V_Buffer[cnt]);
    ADC_GetSqr4Result(&NTC_Buffer[cnt]);
    ADC_GetSqr5Result(&MCUTs_Buffer[cnt]);
    cnt++;
    if (cnt >= ADC_SAMPLE_SIZE)
    {
        cnt = 0;
    }
}
uint32_t Mean_Value_Filter(uint16_t *value, uint32_t size) // 均值滤波
{
    uint32_t sum = 0;
    uint16_t max = 0;
    uint16_t min = 0xffff;
    int i;

    for (i = 0; i < size; i++)
    {
        sum += value[i];
        if (value[i] > max)
        {
            max = value[i];
        }
        if (value[i] < min)
        {
            min = value[i];
        }
    }
    sum -= max + min;
    sum = sum / (size - 2);
    return sum;
}
uint16_t vdda_calibration(void)
{
    float AVCCvalue;
    uint16_t BGR_Code;
    BGR_Code = Mean_Value_Filter(V12V_Buffer, ADC_SAMPLE_SIZE);
    AVCCvalue = ADC_BgrResult2Avcc(BGR_Code);
    return AVCCvalue * 1000; // 1200/meam == vdda/4095
}
uint16_t get_Vref_sampled_value(void)
{
    return Mean_Value_Filter(Vref_Buffer, ADC_SAMPLE_SIZE);
}
uint16_t get_Vout_sampled_value(void)
{
    return Mean_Value_Filter(Vout_Buffer, ADC_SAMPLE_SIZE);
}
uint16_t get_Volt_sampled_value(void)
{
    return Mean_Value_Filter(Volt_Buffer, ADC_SAMPLE_SIZE);
}
uint16_t get_V12V_sampled_value(void)
{
    return Mean_Value_Filter(V12V_Buffer, ADC_SAMPLE_SIZE);
}
uint16_t get_NTC_sampled_value(void)
{
    return Mean_Value_Filter(NTC_Buffer, ADC_SAMPLE_SIZE);
}
uint16_t get_MCUTs_sampled_value(void)
{
    return Mean_Value_Filter(MCUTs_Buffer, ADC_SAMPLE_SIZE);
}
/**
 * @brief Set the overcurr value object
 * 
 * @param threshhold 过流阈值
 */
void set_overcurr_value(uint16_t threshhold)
{
    uint16_t vref_adc;
    uint16_t v1,v1_adc;
    uint16_t th,tl;
    uint16_t vdda = vdda_calibration();
    vref_adc = get_Vref_sampled_value();//运放参考电压adc值
    //把过流值转换成电压     v1 = threshhold * 0.005 * 50
    v1 = threshhold / 4;
    // v1 = v1_adc / 4095 * vdda ;
    v1_adc = v1 * 4095 / vdda;
    th = vref_adc + v1_adc;
    tl = vref_adc - v1_adc;
    CW_ADC->AWDTR = (th << 16) | tl;
    if (overcurr_state == 0)
    {
        overcurr_state = 1;
        ADC_ITConfig(ADC_IT_AWDH | ADC_IT_AWDL, ENABLE);
        ADC_ClearITPendingAll(); // 清除ADC所有中断状态
    }
    //
}
/**
 * @brief 关闭过流检测
 * 
 */
void close_overcurr_detection(void)
{
    ADC_ITConfig(ADC_IT_AWDH | ADC_IT_AWDL, DISABLE);
    overcurr_state = 0;
}
